<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src="/js/jquery.min.js"></script>
<script type="text/javascript">
    var questions="";

    function checkAnswer() {
        var name = checkLoginName();
        var ques =getQuestion();
        var loginName = document.getElementById("loginName");
        var answer = document.getElementById("answer");
        var pass = checkPassword();
        var bools = false;
        if (loginName==""||loginName==null){
            alert("登录名不能为空");
            return false;
        }
        if (loginName.length<6){
            alert("登录名长度不低于6位");
            return false;
        }
        if (answer==""||answer==null){
            alert("答案不能为空");
            return false;
        }
        if (name&&ques&&pass){
            $.ajax({
                url:"/login/checkAnswer",
                type:"post",
                data:{"loginName":loginName,"answer":answer},
                async:false,
                success:function(result){
                    if (result.code==200)
                        bools = true;
                    else
                        alert("答案不正确");
                }
            });
        }
        return bools;
    }
    function checkPassword() {
        var password = document.getElementById("password").value;
        var checkPassword = document.getElementById("checkPassword").value;
        if (password == "" || password == null) {
            alert("密码不为空")
            return false;
        }
        if (password.length < 6) {
            alert("密码长度不低于6位");
            return false;
        }
        if (password == checkPassword) {
            return true;
        }
    }
    function getQuestion() {
        var loginName = document.getElementById("loginName").value;
        var name = checkLoginName();
        var bools = false;
        if (name){
            $.ajax({
                url:"/login/getQuestion",
                type:"post",
                data:{"loginName":loginName},
                async:false,
                success:function(result){
                   questions = result.toString();
                    bools = true;
                }
            });
        }
        return bools;
    }
    function checkLoginName() {
        var loginName = document.getElementById("loginName").value;
        var bools = true;
        if (loginName==""||loginName==null){
            alert("登录名不能为空");
            return false;
        }
        if (loginName.length<6){
            alert("登录名长度不低于6位");
            return false;
        }

        $.ajax({
            url:"/login/checkLoginName",
            type:"post",
            data:{"loginName":loginName},
            async:false,
            success:function(result){
                if (result.code==100){
                    alert("用户不存在");
                    bools= false;
                }
            }
        });
        return bools;
    }


    $(document).ready(function(){
        $(".btn1").click(function(){
            $("p").html("密保："+questions);
        });
    });



</script>
<body>


    <form action="/login/resetPassword">
        输入登录名<input type="text" id="loginName" name="loginName"><br>
        <p>密保:</p> <input type="button" onclick="return getQuestion()" value="获取密保" class="btn1">
        <br>
        答案：<input type="text" name="answer" id="answer"><br>
        新密码<input type="password" name="password" id="password">
        再次输入 <input type="password" name="checkPassword" id="checkPassword">
        <input type="submit" value="找回" onclick="return checkAnswer()"><br>
    </form>
</body>
</html>